<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.TreeSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html lang="zh-CN">
<head>
    <meta charSet="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta http-equiv="Cache-Control" content="no-transform" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta name="referrer" content="never">
    <meta name="renderer" content="webkit" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>小姐姐在线播放</title>
    <link rel="stylesheet" href="style.css">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
</head>
<body>
<section id="main">
    <video id="player" src="https://txmov2.a.yximgs.com/upic/2019/05/26/09/BMjAxOTA1MjYwOTE0NDVfMjk5MDAxMDk0XzEzNDIxOTc1NDE4XzFfMw==_b_B591bd6f52d9049e47c7e8a8a61d299df.mp4?tag=1-1558833601-p-0-rang2wgojb-96c079bdad30fc40" controls webkit-playsinline playsinline></video>
</section>
<section id="buttons">
    <button id="switch">连续: 开</button>
    <button id="next">播放下一个</button>
</section>

<%
    String path = request.getSession().getServletContext().getRealPath("/");
    File file = new File(path, "xjj.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String str = br.readLine();
    ArrayList<String> urlList = new ArrayList<>();
    while(str!=null)
    {
        urlList.add(str);
        str = br.readLine();
    }
    br.close();
    urlList = new ArrayList<>(new TreeSet<String>(urlList));
    request.setAttribute("urlList", urlList);
%>

<script>
    (function (window, document) {

        var getUrlMap = function () {
            var urlString = "${urlList}";
            var urlList = urlString.replace("[","").replace("]", "").split(",");

            var urlMap = {};
            for (var i = 0; i < urlList.length; i++) {
                urlMap[i+1] = urlList[i];
            }
            return urlMap;
        }
        var urlMap = getUrlMap();

        var rd = function (min, max) {
            return Math.floor(Math.random() * (max - min)) + min
        }

        if (top != self) {
            window.top.location.replace(self.location.href);
        }
        var get = function (id) {
            return document.getElementById(id);
        }
        var bind = function (element, event, callback) {
            return element.addEventListener(event, callback);
        }
        var auto = true;
        var player = get('player');
        var randomm = function () {
            var totalCount = Object.keys(urlMap).length;
            var urlKey = rd(1, totalCount);
            console.log("视频总条数-->" + totalCount + ",当前播放-->" + urlKey);
            player.src = urlMap[urlKey];
            player.play();
        }
        bind(get('next'), 'click', randomm);
        bind(player, 'error', function () {
            randomm();
        });
        bind(get('switch'), 'click', function () {
            auto = !auto;
            this.innerText = '连续: ' + (auto ? '开' : '关');
        });
        bind(player, 'ended', function () {
            if (auto) randomm();
        });
    })(window, document);
</script>
</body>
</html>
